package com.isoft.c2team3service2.controller;



import com.isoft.c2team3service2.bean.ResponseData;
import com.isoft.c2team3service2.entity.UserDTO;
import com.isoft.c2team3service2.service.UserFollowUserService;
import com.isoft.c2team3service2.service.impl.BusinessException;
import com.isoft.c2team3service2.util.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/follow")
public class UserFollowController extends BaseController {

    @Autowired
    private UserFollowUserService userFollowUserService;
    /**
     * 关注用户
     */

    @PostMapping("/{followedUserId}")
    public ResponseData<Boolean> followUser(@PathVariable Integer followedUserId) {
        if (UserHolder.getUser() == null) {
            return resultT(false, "请先登录");
        }

        try {
            boolean success = userFollowUserService.followUser(followedUserId);
            return resultT(true, "关注"); // 修改这里，去掉"成功"
        } catch (BusinessException e) {
            return resultT(false, e.getMessage());
        }
    }
    /**
     * 取消关注用户
     */
    @DeleteMapping("/{followedUserId}")
    public ResponseData<Boolean> unfollowUser(@PathVariable Integer followedUserId) {
        if (UserHolder.getUser() == null) {
            return resultT(false, "请先登录");
        }

        boolean success = userFollowUserService.unfollowUser(followedUserId);
        return success ? resultT(true, "取消关注") : resultT(false, "取消关注");
    }

    /**
     * 检查是否关注了某个用户
     */
    @GetMapping("/check/{followedUserId}")
    public ResponseData<Boolean> isFollowingUser(@PathVariable Integer followedUserId) {
        if (UserHolder.getUser() == null) {
            return resultT(false, "请先登录");
        }

        boolean isFollowing = userFollowUserService.isFollowingUser(followedUserId);
        return resultT(isFollowing, isFollowing ? "已关注" : "未关注");
    }

    /**
     * 获取当前用户关注的所有用户
     */
    @GetMapping("/list")
    public ResponseData<List<UserDTO>> getFollowedUsers() {
        if (UserHolder.getUser() == null) {
            return resultT(null, "请先登录");
        }

        List<UserDTO> followedUsers = userFollowUserService.getFollowedUsers();
        return resultT(followedUsers, "查询关注列表");
    }

    /**
     * 获取当前用户的所有粉丝
     */
    @GetMapping("/fans")
    public ResponseData<List<UserDTO>> getFans() {
        if (UserHolder.getUser() == null) {
            return resultT(null, "请先登录");
        }

        List<UserDTO> fans = userFollowUserService.getFans();
        return resultT(fans, "查询粉丝列表");
    }
}